package com.software.controller;

import com.software.dao.ConnectionDao;
import com.software.entity.UserInfo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;


@WebServlet(value = "/userList")
public class TestServlet extends HttpServlet {


    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ConnectionDao connectionDao = new ConnectionDao();
        Connection connection = connectionDao.getConnection();

        //执行sql语句的statement
        Statement statement = null;
        try {
            statement = connection.createStatement();
            //SQL语句
            String sql = "select * from user_info";
            //得到结果
            ResultSet rs = statement.executeQuery(sql);
            List<UserInfo> list = new ArrayList<>();
            while (rs.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(rs.getString("user_id"));
                userInfo.setUserName(rs.getString("user_name"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setAvatar(rs.getString("avatar"));
                list.add(userInfo);
            }

            request.setAttribute("userList",list);
            request.getRequestDispatcher("/userList.jsp").forward(request, response);
//            response.getWriter().write(String.valueOf(list));

        } catch (SQLException e) {
            e.printStackTrace();
        }




    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ConnectionDao connectionDao = new ConnectionDao();
        Connection connection = connectionDao.getConnection();


        //执行sql语句的statement
        Statement statement = null;
        PreparedStatement preparedStatement = null;
        String userName = request.getParameter("userName");
        try {
            statement = connection.createStatement();
            //SQL语句
            String sql = "select * from user_info where user_name like ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "%"+userName+"%");
            //得到结果
            ResultSet rs = preparedStatement.executeQuery();

            List<UserInfo> list = new ArrayList<>();
            while (rs.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(rs.getString("user_id"));
                userInfo.setUserName(rs.getString("user_name"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setAvatar(rs.getString("avatar"));
                list.add(userInfo);
            }

            request.setAttribute("userList",list);
            request.setAttribute("userName",userName);
            request.getRequestDispatcher("/userList.jsp").forward(request, response);
//            response.getWriter().write(String.valueOf(list));

        } catch (SQLException e) {
            e.printStackTrace();
        }




    }




}
